package com.stylefeng.guns.modular.income.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.stylefeng.guns.core.base.controller.BaseController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;

import com.stylefeng.guns.core.log.LogObjectHolder;

import org.springframework.web.bind.annotation.RequestParam;

import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.persistence.model.Income;
import com.stylefeng.guns.modular.income.service.IIncomeService;
import com.stylefeng.guns.modular.system.dao.IncomeDao;

/**
 * 收入管理控制器
 *
 * @author fengshuonan
 * @Date 2021-01-04 19:07:37
 */
@Controller
@RequestMapping("/income")
public class IncomeController extends BaseController {

    private String PREFIX = "/income/income/";

    @Autowired
    private IIncomeService incomeService;
    
    @Autowired
    private IncomeDao incomeDao;

    /**
     * 跳转到收入管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "income.html";
    }

    /**
     * 跳转到添加收入管理
     */
    @RequestMapping("/income_add")
    public String incomeAdd() {
        return PREFIX + "income_add.html";
    }

    /**
     * 跳转到修改收入管理
     */
    @RequestMapping("/income_update/{incomeId}")
    public String incomeUpdate(@PathVariable Integer incomeId, Model model) {
        Income income = incomeService.selectById(incomeId);
        model.addAttribute("item",income);
        LogObjectHolder.me().set(income);
        return PREFIX + "income_edit.html";
    }

    /**
     * 获取收入管理列表
     * @throws ParseException 
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required=false)String name,
    		@RequestParam(required=false) String school,
    		@RequestParam(required=false) Integer level) throws ParseException {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
    	List<Map<String, Object>> lists = incomeDao.selectByCondition(page,name,school,level);
    	List<Map<String, Object>> result = Lists.newArrayList();
    	for (Map<String, Object> map : lists) {
    		Map<String,Object> temp=Maps.newHashMap();
    		temp.put("id", map.get("id"));
    		temp.put("name", map.get("name"));
    		temp.put("age", map.get("age"));
    		temp.put("phone", map.get("phone"));
    		temp.put("money", map.get("money"));
    		temp.put("classNumber", map.get("classNumber"));
    		temp.put("remark", map.get("remark"));
    		if(map.get("registTime")!=null){
    			temp.put("registTime", sdf.format(sdf.parse(map.get("registTime").toString())));
    		}else{
    			temp.put("registTime", "");
    		}
    		temp.put("createTime", map.get("createTime"));
    		if(map.get("type")!=null){
    			if(Integer.valueOf(map.get("type").toString())==1){
    				temp.put("type", "报名费用");
        		}
    			
    			if(Integer.valueOf(map.get("type").toString())==2){
    				temp.put("type", "装备费用");
        		}
    			
    			if(Integer.valueOf(map.get("type").toString())==3){
    				temp.put("type", "报名及装备费用");
        		}
    		}else{
    			temp.put("type", "");
    		}
    		if(map.get("sex")!=null){
    			if(Integer.valueOf(map.get("sex").toString())==1){
    				temp.put("sex", "男");
        		}
    			
    			if(Integer.valueOf(map.get("sex").toString())==2){
    				temp.put("sex", "女");
        		}
    		}else{
    			temp.put("sex", "");
    		}
    		
    		if(map.get("fromTo")!=null){
    			if(Integer.valueOf(map.get("fromTo").toString())==1){
    				temp.put("fromTo", "个人");
        		}
    			
    			if(Integer.valueOf(map.get("fromTo").toString())==2){
    				temp.put("fromTo", "学校");
        		}
    		}else{
    			temp.put("fromTo", "");
    		}
    		
    		if(map.get("level")!=null){
    			if(Integer.valueOf(map.get("level").toString())==1){
    				temp.put("level", "课时数");
        		}
    			
    			if(Integer.valueOf(map.get("level").toString())==2){
    				temp.put("level", "月卡");
        		}
    			if(Integer.valueOf(map.get("level").toString())==3){
    				temp.put("level", "年卡");
        		}
    		}else{
    			temp.put("level", "");
    		}
    		temp.put("school", map.get("school"));
    		result.add(temp);
		}
    	page.setRecords(result);
		System.out.println("总数"+page.getTotal());
        return super.packForBT(page);
    }

    /**
     * 新增收入管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Income income) {
    	income.setCreateTime(new Date());
        incomeService.insert(income);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除收入管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long incomeId) {
        incomeService.deleteById(incomeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改收入管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Income income) {
        incomeService.updateById(income);
        return super.SUCCESS_TIP;
    }

    /**
     * 收入管理详情
     */
    @RequestMapping(value = "/detail/{incomeId}")
    @ResponseBody
    public Object detail(@PathVariable("incomeId") Integer incomeId) {
        return incomeService.selectById(incomeId);
    }
}
